/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MyImagePanel.java
 *
 * Created on 30 août 2011, 10:52:15
 */
package my.monitor;

import java.awt.Point;
import java.beans.PropertyChangeEvent;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import my.monitor.controller.MyController;
import my.monitor.model.MyPosition;
import my.monitor.model.enumeration.CommunicationStatus;
import my.monitor.model.enumeration.MyMode;
import my.monitor.model.enumeration.RobotMissionStatus;
import my.monitor.model.enumeration.VideoStatus;

/**
 *
 * @author pehladik
 */
public class MyImagePanel extends AbstractViewPanel {

    //  The controller used by this view
    private MyController controller;
    //Local
    private Boolean positionComputed = Boolean.valueOf(false);
    boolean objectiveIsSelectable = false;

    public MyImagePanel() {
        initComponents();
    }

    public MyImagePanel(MyController controller) {
        this.controller = controller;
        initComponents();
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                MyController.COMMUNICATION_STATUS_PROPERTY)) {
            CommunicationStatus commStatus = (CommunicationStatus) evt.getNewValue();
            if (commStatus != CommunicationStatus.COMMUNICATION_CONNECTED) {
                this.setVisible(false);
            } else {
                this.setVisible(true);
            }
        } else if (evt.getPropertyName().equals(
                MyController.VIDEO_IMAGE_PROPERTY)) {
            myScreen1.setImg((ImageIcon) evt.getNewValue());
            myScreen1.repaint();
        } else if (evt.getPropertyName().equals(
                MyController.VIDEO_POSITION_PROPERTY)) {
            MyPosition position = (MyPosition) evt.getNewValue();
            jTextX.setText(Integer.toString(position.getX()));
            jTextY.setText(Integer.toString(position.getY()));
            jTextAngle.setText(Integer.toString(position.getAngle()));
        } else if (evt.getPropertyName().equals(
                MyController.VIDEO_POSITION_STATUS_PROPERTY)) {
            positionComputed = (Boolean) evt.getNewValue();
            if (positionComputed.booleanValue()) {
                jButtonComputePosition.setText("Stop computed position");
            } else {
                jButtonComputePosition.setText("Compute position");
            }
        } else if (evt.getPropertyName().equals(
                MyController.CHANGE_ROBOT_OBJECTIVE)) {
            Point p = (Point) evt.getNewValue();
            if (p.x != -1) {
                myScreen1.setObjective(p);
            } else {
                myScreen1.setObjective(null);
            }
            myScreen1.repaint();
        } else if (evt.getPropertyName().equals(
                MyController.CHANGE_MODE_ROBOT_CONTROL)) {
            if ((MyMode) evt.getNewValue() != MyMode.AUTOMATIC) {
                objectiveIsSelectable = false;
                //objective = null;
            } else {
                objectiveIsSelectable = true;
            }
        } else if (evt.getPropertyName().equals(
                MyController.CHANGE_ROBOT_MISSION_STATUS)) {
            System.out.println("view image: change mission");
            if (((RobotMissionStatus) evt.getNewValue() == RobotMissionStatus.NO_MISSION)
                    || ((RobotMissionStatus) evt.getNewValue() == RobotMissionStatus.MISSION_FINISHED)) {
                objectiveIsSelectable = true;
            } else {
                objectiveIsSelectable = false;
            }

            System.out.println("objectable: " + objectiveIsSelectable);
        }
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel14 = new javax.swing.JLabel();
        jTextX = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jTextY = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jTextAngle = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jButtonStartDetectArena = new javax.swing.JButton();
        jButtonComputePosition = new javax.swing.JButton();
        myScreen1 = new my.monitor.MyScreen();

        setMaximumSize(new java.awt.Dimension(680, 610));
        setMinimumSize(new java.awt.Dimension(680, 610));
        setPreferredSize(new java.awt.Dimension(680, 610));

        jLabel14.setFont(new java.awt.Font("Liberation Sans", 1, 14));
        jLabel14.setText("Current Position");

        jTextX.setEditable(false);
        jTextX.setFont(new java.awt.Font("Liberation Sans", 0, 12));

        jLabel15.setFont(new java.awt.Font("Liberation Sans", 0, 12));
        jLabel15.setText("X");

        jTextY.setEditable(false);
        jTextY.setFont(new java.awt.Font("Liberation Sans", 0, 12));

        jLabel16.setFont(new java.awt.Font("Liberation Sans", 0, 12));
        jLabel16.setText("Y");

        jTextAngle.setEditable(false);
        jTextAngle.setFont(new java.awt.Font("Liberation Sans", 0, 12));

        jLabel17.setFont(new java.awt.Font("Liberation Sans", 0, 12));
        jLabel17.setText("Angle");

        jButtonStartDetectArena.setFont(new java.awt.Font("Liberation Sans", 0, 12));
        jButtonStartDetectArena.setText("Detect Arena");
        jButtonStartDetectArena.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonStartDetectArenaMouseClicked(evt);
            }
        });
        jButtonStartDetectArena.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStartDetectArenaActionPerformed(evt);
            }
        });

        jButtonComputePosition.setText("Compute Position");
        jButtonComputePosition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonComputePositionActionPerformed(evt);
            }
        });

        myScreen1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
        myScreen1.setText("myScreen1");
        myScreen1.setPreferredSize(new java.awt.Dimension(640, 480));
        myScreen1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                myScreen1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonComputePosition)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 647, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel15)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextX, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel16)
                                .addGap(3, 3, 3)
                                .addComponent(jTextY, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel17)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextAngle, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel14)))
                    .addComponent(jButtonStartDetectArena, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(myScreen1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 640, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonStartDetectArena)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myScreen1, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(jButtonComputePosition))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16)
                            .addComponent(jLabel17))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15))))
                .addContainerGap(17, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonStartDetectArenaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonStartDetectArenaMouseClicked
}//GEN-LAST:event_jButtonStartDetectArenaMouseClicked

    private void jButtonStartDetectArenaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartDetectArenaActionPerformed
        int test = 1;
        //Custom button text OUI:0 NON:1 CANCEL:2
        while (test == 1) {
            controller.changeArena(VideoStatus.VIDEO_ARENA_CALIBRATION);
            test = JOptionPane.showConfirmDialog(null,
                    "Est-ce que l'arène est délimitée par le carré vert ?",
                    "Calibration",
                    JOptionPane.YES_NO_CANCEL_OPTION);
        }
        if (test == 2) {
            controller.changeArena(VideoStatus.VIDEO_ARENA_KO);
        } else if (test == 0) {
            controller.changeArena(VideoStatus.VIDEO_ARENA_OK);
        }
}//GEN-LAST:event_jButtonStartDetectArenaActionPerformed

    private void jButtonComputePositionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonComputePositionActionPerformed
        if (positionComputed.booleanValue()) {
            controller.changePositionComputed(Boolean.valueOf(false));
        } else {
            controller.changePositionComputed(Boolean.valueOf(true));
        }
}//GEN-LAST:event_jButtonComputePositionActionPerformed

private void myScreen1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myScreen1MouseClicked
    if (objectiveIsSelectable) {
        System.out.println("Bien ciblé sur l'image " + evt.getPoint());
        controller.changeRobotObjective(evt.getPoint());
    }
}//GEN-LAST:event_myScreen1MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonComputePosition;
    private javax.swing.JButton jButtonStartDetectArena;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JTextField jTextAngle;
    private javax.swing.JTextField jTextX;
    private javax.swing.JTextField jTextY;
    private my.monitor.MyScreen myScreen1;
    // End of variables declaration//GEN-END:variables
}
